﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlanetState : MonoBehaviour {


    SpriteRenderer SpriteNow;//星系状态
    GameObject Colonize;
    List<GameObject> Batteries;//建筑槽
    GameObject Re1;//资源槽1
    GameObject Re2;//资源槽2


    void Awake()
    {

        SpriteNow = transform.Find("Sprite").GetComponent<SpriteRenderer>();

        Colonize = transform.Find("Colonize").gameObject;
        Batteries = new List<GameObject>();
        for (int i = 0; i < PlanetRealState.StructLevel; i++)
        {
            Batteries.Add(transform.Find("Colonize/Battery ("+(i+1).ToString()+")").gameObject);
        }
        for (int i = PlanetRealState.StructLevel; i < 3; i++)
        {
            transform.Find("Colonize/Battery (" + (i + 1).ToString() + ")").gameObject.SetActive(false);
        }

        Re1 = transform.Find("Resources/Re1").gameObject;
        Re2 = transform.Find("Resources/Re2").gameObject;

        GetComponent<SpriteRenderer>().sprite = DataCenter.Instance.Planets[Random.Range(0, DataCenter.Instance.Planets.Count)];

        InitState();
    }

    public int Owner;//行星拥有者

    //真实状态
    [System.Serializable]
    public class RealState
    {
        public enum RealType
        {
            OriginCivil,
            Empty
        }
        [SerializeField]
        public RealType Type;//是否土著
        public int StructLevel;//建筑槽数量
    }
    public RealState PlanetRealState;//————————————————————————————

    //可视状态(针对未殖民星球 owner)
    [System.Serializable]
    public class State
    {
        public enum BasicState
        {
            UnObserve,
            Unknown_Observed,
            Observed_Uncontact,
            Observed_empty,
            UnFriendlyCivil
        }
        [SerializeField]
        public BasicState Basic;
    }
    public State StateNow;//————————————————————————————
    public List<State> StateAll;//————————————————————————————

    //殖民（建筑）状态（针对已殖民星球 owner）
    [System.Serializable]
    public class ColonizeState
    {
        public enum StructType
        {
            empty,
            Factory,
            Weapon,
            Raider
        }
        [SerializeField]
        public StructType Struct;
    }
    public List<ColonizeState> Battery;//————————————————————————————
    //殖民（资源）状态
    [System.Serializable]
    public class ResourceState
    {
        public int industry;
        public int weapon;
        public bool HaveRaidar;
    }
    public ResourceState Resource;//————————————————————————————

    void Update()
    {
    }
    public void InitState()
    {
        InitData();
        StateNow = StateAll[ControlCenter.Instance.PlayerIdNow - 1];
        SteState();//设置属性
    }
    void InitData()//初始化数值，每回合进行
    {
        Resource.industry = 0;
        Resource.weapon = 0;
        Resource.HaveRaidar = false;
    }
    void SteState()
    {
        Colonize.SetActive(Owner == ControlCenter.Instance.PlayerIdNow && PlanetRealState.Type != RealState.RealType.OriginCivil);
        Re2.SetActive(Owner == ControlCenter.Instance.PlayerIdNow && PlanetRealState.Type != RealState.RealType.OriginCivil);
        Re1.SetActive(Owner == ControlCenter.Instance.PlayerIdNow);

        Resource.industry = 0;
        if (Owner == ControlCenter.Instance.PlayerIdNow)//星系已殖民
        {
            if (PlanetRealState.Type == RealState.RealType.OriginCivil && Owner == ControlCenter.Instance.PlayerIdNow)//友好文明，无法建造，获得4生产力
            {
                Resource.industry += 4;
                SpriteNow.sprite = DataCenter.Instance.FriendlyCivil;
            }
            else
            {
                SpriteNow.sprite = null;
            }

            for (int i=0;i< PlanetRealState.StructLevel; i++)
            {
                switch (Battery[i].Struct)
                {
                    case ColonizeState.StructType.empty:
                        Batteries[i].GetComponent<SpriteRenderer>().sprite = null;
                        break;
                    case ColonizeState.StructType.Factory:
                        Resource.industry += 1;
                        Batteries[i].GetComponent<SpriteRenderer>().sprite = DataCenter.Instance.Factory;
                        break;
                    case ColonizeState.StructType.Weapon:
                        Resource.weapon += 1;
                        Batteries[i].GetComponent<SpriteRenderer>().sprite = DataCenter.Instance.Weapon;
                        break;
                    case ColonizeState.StructType.Raider:
                        Resource.HaveRaidar = true;
                        Batteries[i].GetComponent<SpriteRenderer>().sprite = DataCenter.Instance.Raider;
                        break;
                    default: break;
                }
            }
            Re1.GetComponentInChildren<TextMesh>().text = Resource.industry.ToString();
            Re2.GetComponentInChildren<TextMesh>().text = Resource.weapon.ToString();

            transform.Find("Comunicate").gameObject.SetActive(ControlCenter.Instance.ExistsContactTarget && Resource.HaveRaidar);
            return;
        }
        switch (StateNow.Basic)//星系未殖民
        {
            case State.BasicState.UnObserve:
                SpriteNow.sprite = null;
                break;
            case State.BasicState.Unknown_Observed:
                SpriteNow.sprite = DataCenter.Instance.UnObserve;
                break;
            case State.BasicState.Observed_Uncontact:
                SpriteNow.sprite = DataCenter.Instance.Observed_Uncontact;
                break;
            case State.BasicState.Observed_empty:
                SpriteNow.sprite = DataCenter.Instance.Observed_empty;
                break;
            case State.BasicState.UnFriendlyCivil:
                SpriteNow.sprite = DataCenter.Instance.UnFriendlyCivil;
                break;
            default: break;
        }
    }
}
